<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: actionGroupExample.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.4</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('actionGroupExample_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">actionGroupExample.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Example of using <a class="el" href="classArActionGroup.html" title="Group a set of ArAction objects together. ">ArActionGroup</a> objects to switch between two different kinds of behavior.This program creates two action groups, a teleoperation group and a wander group. Each group contains actions that together effect the desired behavior: in teleoperation mode, input actions allow the robot to be driven by keyboard or joystick, and higher-priority limiter actions help avoid obstacles. In wander mode, a constant-velocity action drives the robot forward, but higher-priority avoidance actions make the robot turn away from obstacles, or back up if an obstacle is hit or the motors stall. Keyboard commands (the T and W keys) are used to switch between the two modes, by activating the action group for the chosen mode.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="classArActionGroup.html" title="Group a set of ArAction objects together. ">ArActionGroup</a> </dd>
<dd>
<a class="el" href="index.html#actions">Actions</a> overview </dd>
<dd>
actionExample.cpp</dd></dl>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"><a name="_a0"></a><a class="code" href="classArActionGroup.html">ArActionGroup</a> *teleop;</div><div class="line"><a class="code" href="classArActionGroup.html">ArActionGroup</a> *wander;</div><div class="line"></div><div class="line"><span class="comment">// Activate the teleop action group. activateExlcusive() causes</span></div><div class="line"><span class="comment">// all other active action groups to be deactivated.</span></div><div class="line"><span class="keywordtype">void</span> teleopMode(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  teleop-&gt;<a name="a1"></a><a class="code" href="classArActionGroup.html#a94901a8d3aeb7a812c6f2fba86c31977">activateExclusive</a>();</div><div class="line">  printf(<span class="stringliteral">&quot;\n== Teleoperation Mode ==\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   Use the arrow keys to drive, and the spacebar to stop.\n    For joystick control hold the trigger button.\n    Press &#39;w&#39; to switch to wander mode.\n    Press escape to exit.\n&quot;</span>);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Activate the wander action group. activateExlcusive() causes</span></div><div class="line"><span class="comment">// all other active action groups to be deactivated.</span></div><div class="line"><span class="keywordtype">void</span> wanderMode(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  wander-&gt;<a class="code" href="classArActionGroup.html#a94901a8d3aeb7a812c6f2fba86c31977">activateExclusive</a>();</div><div class="line">  printf(<span class="stringliteral">&quot;\n== Wander Mode ==\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;    The robot will now just wander around avoiding things.\n    Press &#39;t&#39; to switch to  teleop mode.\n    Press escape to exit.\n&quot;</span>);</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">  <a name="a2"></a><a class="code" href="classAria.html#ad99c16b5d947229d9f8e1c5b2d4cdd73">Aria::init</a>();</div><div class="line">  <a name="_a3"></a><a class="code" href="classArArgumentParser.html">ArArgumentParser</a> parser(&amp;argc, argv);</div><div class="line">  parser.<a name="a4"></a><a class="code" href="classArArgumentParser.html#a501694b62a7f8b0c3cadd84aeafe1b35">loadDefaultArguments</a>();</div><div class="line">  <a name="_a5"></a><a class="code" href="classArRobot.html">ArRobot</a> robot;</div><div class="line">  <a name="_a6"></a><a class="code" href="classArSonarDevice.html">ArSonarDevice</a> sonar;</div><div class="line"></div><div class="line">  <span class="comment">// Connect to the robot, get some initial data from it such as type and name,</span></div><div class="line">  <span class="comment">// and then load parameter files for this robot.</span></div><div class="line">  <a name="_a7"></a><a class="code" href="classArRobotConnector.html">ArRobotConnector</a> robotConnector(&amp;parser, &amp;robot);</div><div class="line">  <span class="keywordflow">if</span>(!robotConnector.<a name="a8"></a><a class="code" href="classArRobotConnector.html#ab192ac393d9cc1e5b61b1a82b08d085d">connectRobot</a>())</div><div class="line">  {</div><div class="line">    <a name="a9"></a><a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a10"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;actionGroupExample: Could not connect to the robot.&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span>(parser.<a name="a11"></a><a class="code" href="classArArgumentParser.html#ac3889cbdfa862031c51bf80e06954914">checkHelpAndWarnUnparsed</a>())</div><div class="line">    {</div><div class="line">        <span class="comment">// -help not given</span></div><div class="line">        <a name="a12"></a><a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">        <a name="a13"></a><a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (!<a name="a14"></a><a class="code" href="classAria.html#a1b090c01d88bb420b8cf8e0384d25ee1">Aria::parseArgs</a>() || !parser.<a class="code" href="classArArgumentParser.html#ac3889cbdfa862031c51bf80e06954914">checkHelpAndWarnUnparsed</a>())</div><div class="line">  {</div><div class="line">    <a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a15"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;actionGroupExample: Connected to robot.&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">/* - the action group for teleoperation actions: */</span></div><div class="line">  teleop = <span class="keyword">new</span> <a class="code" href="classArActionGroup.html">ArActionGroup</a>(&amp;robot);</div><div class="line"></div><div class="line">  <span class="comment">// don&#39;t hit any tables (if the robot has IR table sensors)</span></div><div class="line">  teleop-&gt;<a name="a16"></a><a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a17"></a><a class="code" href="classArActionLimiterTableSensor.html">ArActionLimiterTableSensor</a>, 100);</div><div class="line"></div><div class="line">  <span class="comment">// limiter for close obstacles</span></div><div class="line">  teleop-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a18"></a><a class="code" href="classArActionLimiterForwards.html">ArActionLimiterForwards</a>(<span class="stringliteral">&quot;speed limiter near&quot;</span>, </div><div class="line">                        300, 600, 250), 95);</div><div class="line"></div><div class="line">  <span class="comment">// limiter for far away obstacles</span></div><div class="line">  teleop-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a class="code" href="classArActionLimiterForwards.html">ArActionLimiterForwards</a>(<span class="stringliteral">&quot;speed limiter far&quot;</span>, </div><div class="line">                           300, 1100, 400), 90);</div><div class="line"></div><div class="line">  <span class="comment">// limiter so we don&#39;t bump things backwards</span></div><div class="line">  teleop-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a19"></a><a class="code" href="classArActionLimiterBackwards.html">ArActionLimiterBackwards</a>, 85);</div><div class="line"></div><div class="line">  <span class="comment">// the joydrive action (drive from joystick)</span></div><div class="line">  <a name="_a20"></a><a class="code" href="classArActionJoydrive.html">ArActionJoydrive</a> joydriveAct(<span class="stringliteral">&quot;joydrive&quot;</span>, 400, 15);</div><div class="line">  teleop-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(&amp;joydriveAct, 50);</div><div class="line"></div><div class="line">  <span class="comment">// the keydrive action (drive from keyboard)</span></div><div class="line">  teleop-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a21"></a><a class="code" href="classArActionKeydrive.html">ArActionKeydrive</a>, 45);</div><div class="line">  </div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">/* - the action group for wander actions: */</span></div><div class="line">  wander = <span class="keyword">new</span> <a class="code" href="classArActionGroup.html">ArActionGroup</a>(&amp;robot);</div><div class="line"></div><div class="line">  <span class="comment">// if we&#39;re stalled we want to back up and recover</span></div><div class="line">  wander-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a22"></a><a class="code" href="classArActionStallRecover.html">ArActionStallRecover</a>, 100);</div><div class="line"></div><div class="line">  <span class="comment">// react to bumpers</span></div><div class="line">  wander-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a23"></a><a class="code" href="classArActionBumpers.html">ArActionBumpers</a>, 75);</div><div class="line"></div><div class="line">  <span class="comment">// turn to avoid things closer to us</span></div><div class="line">  wander-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a24"></a><a class="code" href="classArActionAvoidFront.html">ArActionAvoidFront</a>(<span class="stringliteral">&quot;Avoid Front Near&quot;</span>, 225, 0), 50);</div><div class="line"></div><div class="line">  <span class="comment">// turn avoid things further away</span></div><div class="line">  wander-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a class="code" href="classArActionAvoidFront.html">ArActionAvoidFront</a>, 45);</div><div class="line"></div><div class="line">  <span class="comment">// keep moving</span></div><div class="line">  wander-&gt;<a class="code" href="classArActionGroup.html#a5f3d085b62be2261877f12ff711cab74">addAction</a>(<span class="keyword">new</span> <a name="_a25"></a><a class="code" href="classArActionConstantVelocity.html">ArActionConstantVelocity</a>(<span class="stringliteral">&quot;Constant Velocity&quot;</span>, 400), 25);</div><div class="line"></div><div class="line">  </div><div class="line"></div><div class="line">  <span class="comment">/* - use key commands to switch modes, and use keyboard</span></div><div class="line"><span class="comment">   *   and joystick as inputs for teleoperation actions. */</span></div><div class="line"></div><div class="line">  <span class="comment">// create key handler if Aria does not already have one</span></div><div class="line">  <a name="_a26"></a><a class="code" href="classArKeyHandler.html">ArKeyHandler</a> *keyHandler = <a name="a27"></a><a class="code" href="classAria.html#a5b8042b38b19d6a5d3cfbed4df483e67">Aria::getKeyHandler</a>();</div><div class="line">  <span class="keywordflow">if</span> (keyHandler == NULL)</div><div class="line">  {</div><div class="line">    keyHandler = <span class="keyword">new</span> <a class="code" href="classArKeyHandler.html">ArKeyHandler</a>;</div><div class="line">    <a name="a28"></a><a class="code" href="classAria.html#a9e5496a4ec2c379c8e18706922b57cc1">Aria::setKeyHandler</a>(keyHandler);</div><div class="line">    robot.<a name="a29"></a><a class="code" href="classArRobot.html#ab4ea516667d2358f35b435d97b616ab1">attachKeyHandler</a>(keyHandler);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// set the callbacks</span></div><div class="line">  <a name="_a30"></a><a class="code" href="classArGlobalFunctor.html">ArGlobalFunctor</a> teleopCB(&amp;teleopMode);</div><div class="line">  <a class="code" href="classArGlobalFunctor.html">ArGlobalFunctor</a> wanderCB(&amp;wanderMode);</div><div class="line">  keyHandler-&gt;<a name="a31"></a><a class="code" href="classArKeyHandler.html#ad5442ebb21cd242ab168e94686e06759">addKeyHandler</a>(<span class="charliteral">&#39;w&#39;</span>, &amp;wanderCB);</div><div class="line">  keyHandler-&gt;<a class="code" href="classArKeyHandler.html#ad5442ebb21cd242ab168e94686e06759">addKeyHandler</a>(<span class="charliteral">&#39;W&#39;</span>, &amp;wanderCB);</div><div class="line">  keyHandler-&gt;<a class="code" href="classArKeyHandler.html#ad5442ebb21cd242ab168e94686e06759">addKeyHandler</a>(<span class="charliteral">&#39;t&#39;</span>, &amp;teleopCB);</div><div class="line">  keyHandler-&gt;<a class="code" href="classArKeyHandler.html#ad5442ebb21cd242ab168e94686e06759">addKeyHandler</a>(<span class="charliteral">&#39;T&#39;</span>, &amp;teleopCB);</div><div class="line"></div><div class="line">  <span class="comment">// if we don&#39;t have a joystick, let &#39;em know</span></div><div class="line">  <span class="keywordflow">if</span> (!joydriveAct.<a name="a32"></a><a class="code" href="classArActionJoydrive.html#aadc5a4816e3c69bb0c7b57f72bbadde5">joystickInited</a>())</div><div class="line">    printf(<span class="stringliteral">&quot;Note: Do not have a joystick, only the arrow keys on the keyboard will work.\n&quot;</span>);</div><div class="line">  </div><div class="line">  <span class="comment">// set the joystick so it won&#39;t do anything if the button isn&#39;t pressed</span></div><div class="line">  joydriveAct.<a name="a33"></a><a class="code" href="classArActionJoydrive.html#a2d2e8053a6a49487c5dbeadcbcc44556">setStopIfNoButtonPressed</a>(<span class="keyword">false</span>);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">/* - connect to the robot, then enter teleoperation mode.  */</span></div><div class="line"></div><div class="line">  robot.<a name="a34"></a><a class="code" href="classArRobot.html#a97d57b28549fdef851c00749a97cc776">addRangeDevice</a>(&amp;sonar);</div><div class="line"></div><div class="line">  robot.<a name="a35"></a><a class="code" href="group__easy.html#gaa3657bebab0086dd14ed0c8c3f76dc8b">enableMotors</a>();</div><div class="line">  teleopMode();</div><div class="line">  robot.<a name="a36"></a><a class="code" href="classArRobot.html#a3c1c3056633caf455c8265259f57f6d7">run</a>(<span class="keyword">true</span>);</div><div class="line"></div><div class="line">  <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(0);</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
